package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 检查组控制层
 */
@RestController
@RequestMapping("checkGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
  @Reference
private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增检查组
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
       return checkGroupService.findPage(queryPageBean);
    }

    /**
     * 检查组数据回显
     * @param id
     * @return
     */
    @GetMapping("/findCheckGroupById")
    public Result findCheckGroupById(Integer id) {
        try {
            CheckGroup checkGroup = checkGroupService.findCheckGroupById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 据检查组id查检查项ids
     * @param checkGroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByGroupId")
    public Result findCheckItemIdsByGroupId(Integer checkGroupId) {
        try {
            Integer[] ids = checkGroupService.findCheckItemByGroupId(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.edit(checkGroup, checkitemIds);
            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }

            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkGroupService.delete(id);

            Jedis resource = jedisPool.getResource();
            //表示redis中没有分类信息
            List<Setmeal> setmeal = setmealService.getSetmeal();
            //3.调用dao层的方法到MySQL数据库获取
            if (setmeal!=null && setmeal.size()>0) {
                for (Setmeal s : setmeal) {
                    Setmeal setmeal1 = setmealService.findById(s.getId());
                    //将categoryList集合转换成json字符串，然后存放到redis中
                    resource.set( RedisMessageConstant.SELECT_SETMEAL_RESOURCES+s.getId(), JSON.toJSONString(setmeal1));
                }
            }
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroups = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }


}
